package com.ruoyi.web.controller.mes;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.web.domain.mes.MesMaterial;
import com.ruoyi.web.service.IMesMaterialService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.web.domain.mes.MesMaterialClass;
import com.ruoyi.web.service.IMesMaterialClassService;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 物料分类Controller
 *
 * @author levicash
 * @date 2024-01-10
 */
@RestController
@RequestMapping("/mes/materialclass")
public class MesMaterialClassController extends BaseController {
    @Autowired
    private IMesMaterialClassService mesMaterialClassService;


    /**
     * 查询物料分类列表
     */
    @GetMapping("/list")
    public AjaxResult list(MesMaterialClass mesMaterialClass) {
        List<MesMaterialClass> list = mesMaterialClassService.selectMesMaterialClassList(mesMaterialClass);
        return success(list);
    }

    /**
     * 导出物料分类列表
     */
    @Log(title = "物料分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MesMaterialClass mesMaterialClass) {
        List<MesMaterialClass> list = mesMaterialClassService.selectMesMaterialClassList(mesMaterialClass);
        ExcelUtil<MesMaterialClass> util = new ExcelUtil<MesMaterialClass>(MesMaterialClass.class);
        util.exportExcel(response, list, "物料分类数据");
    }

    /**
     * 获取物料分类详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(mesMaterialClassService.selectMesMaterialClassById(id));
    }

    /**
     * 新增物料分类
     */
    @Log(title = "物料分类", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MesMaterialClass mesMaterialClass) {
        // 所属上级节点没有子节点，但是有物料信息，不允许添加子节点
        MesMaterialClass materialClass = new MesMaterialClass();
        materialClass.setPid(mesMaterialClass.getPid());
        List<MesMaterialClass> list = mesMaterialClassService.selectMesMaterialClassList(materialClass);
        // 查询是否有子节点
        if (list.size() == 0) {
            // 查询自己或者下级是否有物料
            int countNum = mesMaterialClassService.selectRelationCount(mesMaterialClass);
            if (countNum > 0) {
                return AjaxResult.warn("该分类项包含物料信息暂不支持新增子节点");
            }
        }

        return toAjax(mesMaterialClassService.insertMesMaterialClass(mesMaterialClass));
    }

    /**
     * 修改物料分类
     */
    @Log(title = "物料分类", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MesMaterialClass mesMaterialClass) {
        // 查询自己或者下级是否有物料
        int countNum = mesMaterialClassService.selectRelationCount(mesMaterialClass);
        if (countNum > 0) {
            return AjaxResult.warn("该分类项包含物料信息暂不支持编辑");
        }
        return toAjax(mesMaterialClassService.updateMesMaterialClass(mesMaterialClass));
    }

    /**
     * 删除物料分类
     */
    @Log(title = "物料分类", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        if (ids.length > 1) {
            return AjaxResult.warn("请选择一项进行删除");
        }
        MesMaterialClass mode = mesMaterialClassService.selectMesMaterialClassById(ids[0]);
        MesMaterialClass materialClass = new MesMaterialClass();
        materialClass.setPid(mode.getId());
        List<MesMaterialClass> list = mesMaterialClassService.selectMesMaterialClassList(materialClass);
        // 查询是否有子节点
        if (list.size() > 0) {
            return AjaxResult.warn("该项包含子节点，请先删除子节点");
        }
        MesMaterialClass findMode = mesMaterialClassService.selectMesMaterialClassById(ids[0]);
        // 查询自己或者下级是否有物料
        int countNum = mesMaterialClassService.selectRelationCount(findMode);
        if (countNum > 0) {
            return AjaxResult.warn("该分类项包含物料信息暂不支持删除");
        }
        return toAjax(mesMaterialClassService.deleteMesMaterialClassByIds(ids));
    }
}
